﻿using System.Web;

namespace CH.FourBoards.LagerWalti.FrontEnd.Util
{
	public class UrlGenerator
	{
		private readonly HttpContext _context;

		public UrlGenerator(HttpContext context)
		{
			_context = context;
		}

		public string Current
		{
			get { return _context.Request.Url.OriginalString; }
		}

		public string ToAbsolute(string relativeUrl)
		{
			var url = NormalizeRelative(relativeUrl);
			var request = _context.Request;
			var protocol = (request.IsSecureConnection) ? "https" : "http";
			var host = request.Headers["Host"];
			var path = VirtualPathUtility.ToAbsolute(url);

			return string.Format("{0}://{1}{2}", protocol, host, path);
		}

		private string NormalizeRelative(string url)
		{
			if (url.StartsWith("~")) {
				return url;
			}
			return "~/" + url;
		}
	}
}